package com.emm.yixun.report.controller;

import java.math.RoundingMode;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.emm.yixun.report.domain.ExpectedEarlyWarning;
import com.emm.yixun.report.domain.SalesInfo;
import com.emm.yixun.report.domain.TransactionListings;
import com.emm.yixun.report.domain.TransactionListingsOverlapping;
import com.emm.yixun.report.domain.dto.BrokerRankingEntity;
import com.emm.yixun.report.domain.dto.CityTransactions;
import com.emm.yixun.report.domain.dto.IndustrySalesAccountingDtoRes;
import com.emm.yixun.report.domain.dto.KeyProjectsDtoReq;
import com.emm.yixun.report.domain.dto.KeyProjectsDtoRes;
import com.emm.yixun.report.domain.dto.ProjectCityMonthDtoRes;
import com.emm.yixun.report.domain.dto.TransactionListingsDtoRes;
import com.emm.yixun.report.mapper.BrokerRankingMapper;
import com.emm.yixun.report.mapper.CityHotMapper;
import com.emm.yixun.report.mapper.IndustrySalesAccountingMapper;
import com.emm.yixun.report.mapper.KeyCityTrunMapper;
import com.emm.yixun.report.mapper.KeyProjectsMapper;
import com.emm.yixun.report.mapper.ProjectCityMonthMapper;
import com.emm.yixun.report.mapper.TransactionListingsMapper;
import com.emm.yixun.report.service.IBrokerRankingService;
import com.emm.yixun.report.service.IExpectedEarlyWarningService;
import com.emm.yixun.report.service.ISalesInfoService;
import com.emm.yixun.report.service.ITransactionListingsOverlappingService;
import com.emm.yixun.report.service.ITransactionListingsService;

@RestController
@RequestMapping("/reportApi")
public class ReportApiRestController {
	@Autowired
	private ISalesInfoService salesInfoService;
	@Autowired
	private CityHotMapper cityHotMapper;
	@Autowired
	private KeyProjectsMapper keyProjectsMapper;
	@Autowired
	private KeyCityTrunMapper keyCityTrunMapper;
	@Autowired
	private ProjectCityMonthMapper projectCityMonthMapper;
	@Autowired
	private IndustrySalesAccountingMapper industrySalesAccountingMapper;
	@Autowired
	private IExpectedEarlyWarningService iExpectedEarlyWarningService;
	@Autowired
	private ITransactionListingsService iTransactionListingsService;
	@Autowired
	private TransactionListingsMapper transactionListingsMapper;
	@Autowired
	private ITransactionListingsOverlappingService iTransactionListingsOverlappingService;
	@Autowired
	private BrokerRankingMapper brokerRankingMapper;
	@Autowired
	private IBrokerRankingService iBrokerRankingService;
	
	@Autowired
	@Qualifier("m2JdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	/**
	 * 整体营销轮盘图 入参商户Id
	 * 
	 * @return 商户下统计数据
	 */
	@RequestMapping("/salesInfo")
	public SalesInfo queryMonitorByMerchantId(String merchantId, String busType) {
		SalesInfo salesInfo = null;
		try {
			SalesInfo entity = new SalesInfo();
			entity.setMerchantId(Long.valueOf(merchantId));
			entity.setType(busType);
			salesInfo = salesInfoService.selectOne(entity);
			if (salesInfo != null) {
				/** 完成占比：总签约金额 / 全年指标 */
				if (salesInfo.getTotalSignAmount() != null && salesInfo.getSalesTarget() != null) {
					salesInfo.setCompleteAccount(String.valueOf(salesInfo.getTotalSignAmount().divide(salesInfo.getSalesTarget(), 3,RoundingMode.HALF_UP)));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return salesInfo;
	}

	/**
	 * 日期查询认购，签约，回款金额 入参商户Id、开始时间、结束时间
	 * 
	 * @return 商户下查询数据
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/timesInfo")
    public Map<String,Object> queryTimeByMerchantId(final String merchantId,final String busType, final String startTime,final String endTime){
		Map<String,Object> resultMap=new HashMap<String, Object>();
		try {
			CallableStatementCreator cc=new CallableStatementCreator() {
				@Override
				public CallableStatement createCallableStatement(Connection con)
						throws SQLException {
					   String storedProc = "{call proc_sales_screen (?,?,?,?,?,?)}";// 调用的sql   
		               CallableStatement cs = con.prepareCall(storedProc);   
		               cs.setString(1, merchantId);  
		               if("jr".equals(busType)){//今日
		            	   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		            	   String dateString = formatter.format(new Date());
		            	   cs.setString(2, dateString);  
			               cs.setString(3, dateString);  
		               }else if("bz".equals(busType)){//本周
		            	   Calendar cal = Calendar.getInstance();
			           	   int date = cal.get(Calendar.DAY_OF_MONTH);
			           	   int n = cal.get(Calendar.DAY_OF_WEEK);
			           	   if (n == 1) {
			           		  n = 7;
			           	   } else {
			           		  n = n - 1;
			           	   }
			           	   // 日期格式
			           	   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			           	   cal.set(Calendar.DAY_OF_MONTH, date + 1 - n);//本周第一天
		            	   cs.setString(2, sdf.format(cal.getTime()));  
		            	   cal.set(Calendar.DAY_OF_MONTH, date + 7 - n);//本周最后一天
			               cs.setString(3, sdf.format(cal.getTime()));  
		               }else if("qt".equals(busType)){//其它
		            	   cs.setString(2, startTime);  
			               cs.setString(3, endTime);  
		               }
		               cs.registerOutParameter(4,java.sql.Types.DECIMAL);// 注册输出参数的类型   
		               cs.registerOutParameter(5,java.sql.Types.DECIMAL);// 注册输出参数的类型  
		               cs.registerOutParameter(6,java.sql.Types.DECIMAL);// 注册输出参数的类型  
		               return cs;   
				}
			};
			CallableStatementCallback csc=new CallableStatementCallback() {   
	             public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {   
	               cs.execute();   
	               Map<String,Object> callMap=new HashMap<String, Object>();
	               callMap.put("signAmount", cs.getDouble(4));
	               callMap.put("subscribeAmount", cs.getDouble(5));
	               callMap.put("receivedAmount", cs.getDouble(6));
	               return callMap;// 获取输出参数的值
	             }
	        }; 
	        resultMap=(Map<String,Object>)jdbcTemplate.execute(cc, csc);
	        
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
    }
	
	/**
	 * 城市成交热力图【各项目认购、签约套数及金额】
	 * @param merchantId
	 * @return
	 */
	@RequestMapping("/projectTransactionsInfo")
	public List<CityTransactions> queryProjectTransactions(String merchantId) {
		List<CityTransactions> projectList = new ArrayList<CityTransactions>();
		try {
			List<CityTransactions> projectTranList = cityHotMapper.selectProjectTransactions(Long.valueOf(merchantId));
			if(projectTranList != null && projectTranList.size() > 0){
				for(CityTransactions proEntity : projectTranList){
					CityTransactions proTran = new CityTransactions();
					proTran.setProjectName(proEntity.getProjectName());
					proTran.setSubscribeNumber(proEntity.getSubscribeNumber());
					proTran.setSubscribeAmount(proEntity.getSubscribeAmount());
					proTran.setSignNumber(proEntity.getSignNumber());
					proTran.setSignAmount(proEntity.getSignAmount());
					projectList.add(proTran);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return projectList;
	}
	
	/**
	 * 城市成交热力图【各城市认购、签约套数及金额】
	 * @param merchantId
	 * @return
	 */
	@RequestMapping("/cityTransactionsInfo")
	public List<CityTransactions> queryCityTransactions(String merchantId) {
		List<CityTransactions> cityList = new ArrayList<CityTransactions>();
		try {
			List<CityTransactions> cityTranList = cityHotMapper.selectCityTransactions(Long.valueOf(merchantId));
			if(cityTranList != null && cityTranList.size() > 0){
				for(CityTransactions cityEntity : cityTranList){
					CityTransactions cityTran = new CityTransactions();
					cityTran.setCityName(cityEntity.getCityName());
					cityTran.setSubscribeNumber(cityEntity.getSubscribeNumber());
					cityTran.setSubscribeAmount(cityEntity.getSubscribeAmount());
					cityTran.setSignNumber(cityEntity.getSignNumber());
					cityTran.setSignAmount(cityEntity.getSignAmount());
					cityList.add(cityTran);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cityList;
	}
	
	/** 
	 * 重点项目数据监控
	 * @param merchantId
	 * @param projectId
	 * @return
	 */
	@RequestMapping("/projectDataMonitorInfo")
	public KeyProjectsDtoRes queryProjectDataMonitor(String merchantId, String projectId) {
		KeyProjectsDtoRes keyProjectsDtoRes = new KeyProjectsDtoRes();
		try {
			KeyProjectsDtoReq keyProjectsDtoReq = new KeyProjectsDtoReq();
			keyProjectsDtoReq.setMerchantId(merchantId);
			keyProjectsDtoReq.setProjectId(projectId);
			keyProjectsDtoRes = keyProjectsMapper.selectProjectDataMonitor(keyProjectsDtoReq);
			if(null==keyProjectsDtoRes)keyProjectsDtoRes = new KeyProjectsDtoRes();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return keyProjectsDtoRes;
	}

	/**
	 * 各城市到访转化率
	 * @param merchantId
	 * @return
	 */
	@RequestMapping("/cityVisitConversionInfo")
	public List<KeyProjectsDtoRes> queryCityVisitConversion(String merchantId, String months) {
		List<KeyProjectsDtoRes> cityList = new ArrayList<KeyProjectsDtoRes>();
		try {
			List<KeyProjectsDtoRes> cityVisitList = keyCityTrunMapper.selectCityVisitConversion(Long.valueOf(merchantId), months);
			if(cityVisitList != null && cityVisitList.size() > 0){
				for(KeyProjectsDtoRes cityEntity : cityVisitList){
					KeyProjectsDtoRes keyProjectsDtoRes = new KeyProjectsDtoRes();
					keyProjectsDtoRes.setCityName(cityEntity.getCityName());
					keyProjectsDtoRes.setSubscribeNum(cityEntity.getSubscribeNum());
					keyProjectsDtoRes.setSignNum(cityEntity.getSignNum());
					keyProjectsDtoRes.setVisitNum(cityEntity.getVisitNum());
					keyProjectsDtoRes.setConversionNum(cityEntity.getConversionNum());
					cityList.add(keyProjectsDtoRes);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cityList;
	}
	
	/**
	 * 各城市公司指标达成率【年度、月度】 （认购）
	 * @param merchantId
	 * @return
	 */
	@RequestMapping("/cityReachRateSubscribeInfo")
	public List<ProjectCityMonthDtoRes> queryCityReachRateSubscribe(String merchantId) {
		List<ProjectCityMonthDtoRes> cityList = new ArrayList<ProjectCityMonthDtoRes>();
		try {
			List<ProjectCityMonthDtoRes> projectCityList_sub = projectCityMonthMapper.selectCityReachRateSubscribe(Long.valueOf(merchantId));
			if(projectCityList_sub != null && projectCityList_sub.size() > 0){
				for(ProjectCityMonthDtoRes cityEntity : projectCityList_sub){
					ProjectCityMonthDtoRes projectCityMonthDtoRes = new ProjectCityMonthDtoRes();
					projectCityMonthDtoRes.setCityName(cityEntity.getCityName());
					projectCityMonthDtoRes.setYearNum(cityEntity.getYearNum());
					projectCityMonthDtoRes.setMonthNum(cityEntity.getMonthNum());
					projectCityMonthDtoRes.setReachRateNum(cityEntity.getReachRateNum());
					cityList.add(projectCityMonthDtoRes);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cityList;
	}
	
	/**
	 * 各城市公司指标达成率【年度、月度】 （签约）
	 * @param merchantId
	 * @return
	 */
	@RequestMapping("/cityReachRateSignInfo")
	public List<ProjectCityMonthDtoRes> queryCityReachRateSign(String merchantId) {
		List<ProjectCityMonthDtoRes> cityList_qy = new ArrayList<ProjectCityMonthDtoRes>();
		try {
			List<ProjectCityMonthDtoRes> projectCityList_sign = projectCityMonthMapper.selectCityReachRateSign(Long.valueOf(merchantId));
			if(projectCityList_sign != null && projectCityList_sign.size() > 0){
				for(ProjectCityMonthDtoRes cityEntity : projectCityList_sign){
					ProjectCityMonthDtoRes projectCityMonthDtoRes_qy = new ProjectCityMonthDtoRes();
					projectCityMonthDtoRes_qy.setCityName(cityEntity.getCityName());
					projectCityMonthDtoRes_qy.setYearNum(cityEntity.getYearNum());
					projectCityMonthDtoRes_qy.setMonthNum(cityEntity.getMonthNum());
					projectCityMonthDtoRes_qy.setReachRateNum(cityEntity.getReachRateNum());
					cityList_qy.add(projectCityMonthDtoRes_qy);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cityList_qy;
	}
	
	/**
	 * 各城市公司指标达成率【年度、月度】 （回款）
	 * @param merchantId
	 * @return
	 */
	@RequestMapping("/cityReachRatePaymentInfo")
	public List<ProjectCityMonthDtoRes> queryCityReachRatePayment(String merchantId) {
		List<ProjectCityMonthDtoRes> cityList_hk = new ArrayList<ProjectCityMonthDtoRes>();
		try {
			List<ProjectCityMonthDtoRes> projectCityList_payment = projectCityMonthMapper.selectCityReachRatePayment(Long.valueOf(merchantId));
			if(projectCityList_payment != null && projectCityList_payment.size() > 0){
				for(ProjectCityMonthDtoRes cityEntity : projectCityList_payment){
					ProjectCityMonthDtoRes projectCityMonthDtoRes_hk = new ProjectCityMonthDtoRes();
					projectCityMonthDtoRes_hk.setCityName(cityEntity.getCityName());
					projectCityMonthDtoRes_hk.setYearNum(cityEntity.getYearNum());
					projectCityMonthDtoRes_hk.setMonthNum(cityEntity.getMonthNum());
					projectCityMonthDtoRes_hk.setReachRateNum(cityEntity.getReachRateNum());
					cityList_hk.add(projectCityMonthDtoRes_hk);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cityList_hk;
	}
	
	/**
	 * 各业态累计销售金额及比例
	 * @param merchantId
	 * @return
	 */
	@RequestMapping("/eachFormatCumulativeInfo")
	public List<IndustrySalesAccountingDtoRes> selectEachFormatCumulative(String merchantId, String months) {
		List<IndustrySalesAccountingDtoRes> industrySalesList = new ArrayList<IndustrySalesAccountingDtoRes>();
		try {
			if("".equals(months)){
				months = null;
			}
			List<IndustrySalesAccountingDtoRes> industrySalesAccountingDtoResList = industrySalesAccountingMapper.selectEachFormatCumulative(Long.valueOf(merchantId),months);
			if(industrySalesAccountingDtoResList != null && industrySalesAccountingDtoResList.size() > 0){
				for(IndustrySalesAccountingDtoRes industrySalesEntity : industrySalesAccountingDtoResList){
					IndustrySalesAccountingDtoRes industrySalesAccountingDtoRes = new IndustrySalesAccountingDtoRes();
					industrySalesAccountingDtoRes.setPropertyType(industrySalesEntity.getPropertyType());
//					industrySalesAccountingDtoRes.setSalesNum(industrySalesEntity.getSalesNum());
					industrySalesAccountingDtoRes.setAmount(industrySalesEntity.getAmount());
					industrySalesList.add(industrySalesAccountingDtoRes);
				}
			}else{
				IndustrySalesAccountingDtoRes industrySalesAccountingDtoRes = new IndustrySalesAccountingDtoRes();
				industrySalesAccountingDtoRes.setPropertyType("公寓");
				industrySalesAccountingDtoRes.setAmount(0);
				industrySalesList.add(industrySalesAccountingDtoRes);
				IndustrySalesAccountingDtoRes industrySalesAccountingDtoRes2 = new IndustrySalesAccountingDtoRes();
				industrySalesAccountingDtoRes2.setPropertyType("别墅");
				industrySalesAccountingDtoRes2.setAmount(0);
				industrySalesList.add(industrySalesAccountingDtoRes2);
				IndustrySalesAccountingDtoRes industrySalesAccountingDtoRes3 = new IndustrySalesAccountingDtoRes();
				industrySalesAccountingDtoRes3.setPropertyType("车位");
				industrySalesAccountingDtoRes3.setAmount(0);
				industrySalesList.add(industrySalesAccountingDtoRes3);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return industrySalesList;
	}
	
	/**
	 * 未收款预警 入参商户Id
	 * 
	 * @return 商户下统计数据
	 */
	@RequestMapping("/expectedEarlyWarningInfo")
	public List<ExpectedEarlyWarning> queryExpectedEarlyWarningInfo(String merchantId) {
		List<ExpectedEarlyWarning> expectedEarlyWarningList = new ArrayList<ExpectedEarlyWarning>();;
		try {
			EntityWrapper<ExpectedEarlyWarning> expectedEarlyWarning = new EntityWrapper<ExpectedEarlyWarning>();
			ExpectedEarlyWarning entity = new ExpectedEarlyWarning();
			entity.setMerchantId(Long.valueOf(merchantId));
			entity.setDelFlag("0");
			expectedEarlyWarning.setEntity(entity);
			List<ExpectedEarlyWarning> resultList = iExpectedEarlyWarningService.selectList(expectedEarlyWarning);
			if (resultList != null && resultList.size() > 0) {
				for(ExpectedEarlyWarning expectedEarlyWarningEntity : resultList){
					ExpectedEarlyWarning res = new ExpectedEarlyWarning();
					if("1".equals(expectedEarlyWarningEntity.getType())){
						res.setType("安全");
					}else if("2".equals(expectedEarlyWarningEntity.getType())){
						res.setType("预警");
					}else if("3".equals(expectedEarlyWarningEntity.getType())){
						res.setType("风险");
					}else if("4".equals(expectedEarlyWarningEntity.getType())){
						res.setType("高风险");
					}
					res.setNumber(expectedEarlyWarningEntity.getNumber());
					expectedEarlyWarningList.add(res);
				}
			}else{
				//无数据，初始化默认数据
				ExpectedEarlyWarning res_1 = new ExpectedEarlyWarning();
				ExpectedEarlyWarning res_2 = new ExpectedEarlyWarning();
				ExpectedEarlyWarning res_3 = new ExpectedEarlyWarning();
				ExpectedEarlyWarning res_4 = new ExpectedEarlyWarning();
				res_1.setType("安全");
				res_1.setNumber(0);
				res_2.setType("预警");
				res_2.setNumber(0);
				res_3.setType("风险");
				res_3.setNumber(0);
				res_4.setType("高风险");
				res_4.setNumber(0);
				expectedEarlyWarningList.add(res_1);
				expectedEarlyWarningList.add(res_2);
				expectedEarlyWarningList.add(res_3);
				expectedEarlyWarningList.add(res_4);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return expectedEarlyWarningList;
	}
	
	/**
	 * 成交房源分析，获取下拉城市列表选项 入参商户Id
	 * 
	 * @return 城市列表
	 */
	@RequestMapping("/cityListInfo")
	public List<TransactionListings> querycityListInfo(String merchantId) {
		List<TransactionListings> transactionListingsList = new ArrayList<TransactionListings>();
		try {
			EntityWrapper<TransactionListings> entityWrapper = new EntityWrapper<TransactionListings>();
			TransactionListings entity = new TransactionListings();
			entity.setMerchantId(Long.valueOf(merchantId));
			entityWrapper.setEntity(entity);
			List<TransactionListings> cityList = iTransactionListingsService.selectList(entityWrapper);
			if (cityList != null && cityList.size() > 0) {
				for(TransactionListings transactionListings : cityList){
					TransactionListings res = new TransactionListings();
					res.setCityId(transactionListings.getCityId());
					res.setCityName(transactionListings.getCityName());
					transactionListingsList.add(res);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return transactionListingsList;
	}

	/**
	 * 成交房源分析【面积段】，获取下拉城市列表选项 入参商户Id
	 * 
	 * @return 城市列表
	 */
	@RequestMapping("/transactionListingsAreaInfo")
	public List<TransactionListingsDtoRes> queryTransactionListingsAreaInfo(String merchantId, String cityId) {
			List<TransactionListingsDtoRes> transactionListingsDtoRes = new ArrayList<TransactionListingsDtoRes>();
			try{
				if(cityId != null && !"".equals(cityId)){
				EntityWrapper<TransactionListings> entityWrapper = new EntityWrapper<TransactionListings>();
				TransactionListings entity = new TransactionListings();
				entity.setMerchantId(Long.valueOf(merchantId));
				entity.setCityId(Long.valueOf(cityId));
				entityWrapper.setEntity(entity);
				List<TransactionListings> cityList = iTransactionListingsService.selectList(entityWrapper);
				if (cityList != null && cityList.size() > 0) {
					for(TransactionListings transactionListings : cityList){
						TransactionListingsDtoRes res = new TransactionListingsDtoRes();
						res.setAreaNum(String.valueOf(transactionListings.getLtSixTenM()+","+transactionListings.getGtSixTenLtEightTenM()+
								","+transactionListings.getGtEightTenLtNineTenM()+","+transactionListings.getGtNineTenLtTenTenM()+
								","+transactionListings.getGtTenTenLtTenOneM()+","+transactionListings.getGtTenOneLtTenTowM()+
								","+transactionListings.getGtTenTwoLtTenFourM()+","+transactionListings.getGtTenFourM()));
						transactionListingsDtoRes.add(res);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return transactionListingsDtoRes;
	}
	
	/**
	 * 成交房源分析【总价段】，获取下拉城市列表选项 入参商户Id
	 * 
	 * @return 城市列表
	 */
	@RequestMapping("/transactionListingsSumPriceInfo")
	public List<TransactionListingsDtoRes> queryTransactionListingsSumPriceInfo(String merchantId, String cityId) {
			List<TransactionListingsDtoRes> transactionListingsDtoRes = new ArrayList<TransactionListingsDtoRes>();
			try{
				if(cityId != null && !"".equals(cityId)){
				EntityWrapper<TransactionListings> entityWrapper = new EntityWrapper<TransactionListings>();
				TransactionListings entity = new TransactionListings();
				entity.setMerchantId(Long.valueOf(merchantId));
				entity.setCityId(Long.valueOf(cityId));
				entityWrapper.setEntity(entity);
				List<TransactionListings> cityList = iTransactionListingsService.selectList(entityWrapper);
				if (cityList != null && cityList.size() > 0) {
					for(TransactionListings transactionListings : cityList){
						TransactionListingsDtoRes res = new TransactionListingsDtoRes();
						res.setAreaNum(String.valueOf(transactionListings.getLtFiveTenA()+","+transactionListings.getGtFiveTenLtSixTenA()+
								","+transactionListings.getGtSixTenLtSevenTenA()+","+transactionListings.getGtSevenTenLtNineTenA()+
								","+transactionListings.getGtNineTenLtTenOneA()+","+transactionListings.getGtTenOneLtTenTreeA()+
								","+transactionListings.getGtTenTreeLtTenSixA()+","+transactionListings.getGtTenSixA()));
						transactionListingsDtoRes.add(res);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return transactionListingsDtoRes;
	}
	
	/**
	 * 面积总价段交叉分析，获取下拉城市列表选项 入参商户Id
	 * 
	 * @return 城市列表
	 */
	@RequestMapping("/overlappingCityListInfo")
	public List<TransactionListingsOverlapping> queryOverlappingCityListInfo(String merchantId) {
		List<TransactionListingsOverlapping> transactionListingsOverlappingList = new ArrayList<TransactionListingsOverlapping>();
		try {
			EntityWrapper<TransactionListingsOverlapping> entityWrapper = new EntityWrapper<TransactionListingsOverlapping>();
			TransactionListingsOverlapping entity = new TransactionListingsOverlapping();
			entity.setMerchantId(Long.valueOf(merchantId));
			entityWrapper.setEntity(entity);
			List<TransactionListingsOverlapping> cityList = iTransactionListingsOverlappingService.selectList(entityWrapper);
			if (cityList != null && cityList.size() > 0) {
				for(TransactionListingsOverlapping transactionListingsOverlapping : cityList){
					TransactionListingsOverlapping res = new TransactionListingsOverlapping();
					res.setCityId(transactionListingsOverlapping.getCityId());
					res.setCityName(transactionListingsOverlapping.getCityName());
					transactionListingsOverlappingList.add(res);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return transactionListingsOverlappingList;
	}
	
	/**
	 * 面积总价段交叉分析，获取下拉城市列表选项 入参商户Id
	 * 
	 * @return 城市列表
	 */
	@RequestMapping("/transactionListingsOverlappingInfo")
	public List<TransactionListingsDtoRes> queryTransactionListingsOverlappingInfo(String merchantId, String cityId) {
			List<TransactionListingsDtoRes> transactionListingsDtoRes = new ArrayList<TransactionListingsDtoRes>();
			try{
				if(cityId != null && !"".equals(cityId)){
				EntityWrapper<TransactionListingsOverlapping> entityWrapper = new EntityWrapper<TransactionListingsOverlapping>();
				TransactionListingsOverlapping entity = new TransactionListingsOverlapping();
				entity.setMerchantId(Long.valueOf(merchantId));
				entity.setCityId(Long.valueOf(cityId));
				entityWrapper.setEntity(entity);
				List<TransactionListingsOverlapping> cityList = iTransactionListingsOverlappingService.selectList(entityWrapper);
				if (cityList != null && cityList.size() > 0) {
					for(TransactionListingsOverlapping transactionListingsOverlapping : cityList){
						TransactionListingsDtoRes res = new TransactionListingsDtoRes();
						res.setAreaNum(String.valueOf(transactionListingsOverlapping.getLtSixTenMA()+","+transactionListingsOverlapping.getGtSixTenLtEightTenMA()+
								","+transactionListingsOverlapping.getGtEightTenLtNineTenMA()+","+transactionListingsOverlapping.getGtNineTenLtTenTenMA()+
								","+transactionListingsOverlapping.getGtTenTenLtTenOneMA()+","+transactionListingsOverlapping.getGtTenOneLtTenTowMA()+
								","+transactionListingsOverlapping.getGtTenTwoLtTenFourMA()+","+transactionListingsOverlapping.getGtTenFourMA()));
						transactionListingsDtoRes.add(res);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return transactionListingsDtoRes;
	}
	
	/**
	 * 经纪人业绩排行榜
	 * @param merchantId
	 * @return
	 */
	@RequestMapping("/brokerRankingInfo")
	public List<BrokerRankingEntity> queryBrokerRankingInfo(String merchantId, String cityId) {
		List<BrokerRankingEntity> brokerRankingList = new ArrayList<BrokerRankingEntity>();
		try {
			if("".equals(cityId)){
				cityId = null;
			}
			List<BrokerRankingEntity> brList = brokerRankingMapper.selectBrokerRankingInfo(Long.valueOf(merchantId), cityId);
			if(brList != null && brList.size() > 0){
				for(BrokerRankingEntity proEntity : brList){
					BrokerRankingEntity brokerRanking = new BrokerRankingEntity();
					brokerRanking.setUserName(proEntity.getUserName());
					brokerRanking.setSubscribeNumber(proEntity.getSubscribeNumber());
					brokerRanking.setSubscribeAmount(proEntity.getSubscribeAmount());
					brokerRanking.setSignNumber(proEntity.getSignNumber());
					brokerRanking.setSignAmount(proEntity.getSignAmount());
					brokerRankingList.add(brokerRanking);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return brokerRankingList;
	}
	
	/**
	 * 经纪人业绩排行榜【条件查询】 - 查询当前商户下所有城市列表
	 * @param merchantId
	 * @return
	 */
	@RequestMapping("/cityListByMerchantIdInfo")
	public List<BrokerRankingEntity> queryCityListByMerchantId(String merchantId) {
		List<BrokerRankingEntity> brokerRankingList = new ArrayList<BrokerRankingEntity>();
		try {
			List<BrokerRankingEntity> brList = brokerRankingMapper.selectCityListByMerchantId(Long.valueOf(merchantId));
			if(brList != null && brList.size() > 0){
				for(BrokerRankingEntity brEntity : brList){
					BrokerRankingEntity brokerRanking = new BrokerRankingEntity();
					brokerRanking.setCityId(brEntity.getCityId());
					brokerRanking.setCityName(brEntity.getCityName());
					brokerRankingList.add(brokerRanking);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return brokerRankingList;
	}
	
}
